var basicInfomation = document.getElementById("basicInfomation").children[0];
var basicInfomationData = [
    ["委托单位", "项目名称","产品名称","检测部位", "检测时机", "焊接方法", "坡口形式", "设备型号", "焦点尺寸", "像质计型号",
        "增感方式", "胶片牌号", "胶片规格", "显影时间", "洗片条件", "标准黑度范围", "检测标准", "合格级别", "热处理状态",
        "材质", "设备编号", "显影液", "显影温度", "技术等级", "验收标准", "检测比例", "编制人员级别", "备注"],
    []];
var r_id = GetQueryString("id");
var basicInfoID = [
    "r_company", "r_pname","task_product","r_position", "r_opportunity", "r_weldM", "r_type",
    "r_equ", "r_focus", "r_IQIV", "r_sensitizer", "r_filmBrand", "r_filmV",
    "r_devTime", "r_devCondition", "r_b", "r_standard", "r_qua", "r_heat",
    "r_texture", "r_equid", "r_develop", "r_devT", "r_tlevel", "r_check", "r_scale", "r_level", "r_remark"
];
var postData;
$.ajax({
    url: '/echo/report',
    type: 'post',
    data: {
        "requestType": "workpieceData",
        "r_id": r_id
    },
    async: false,
    success: function (data, status) {
        if (status == 'success') {
            postData = data;
            basicInfomationData[1] = [
                data["base"][0]["r_company"],
                data["base"][0]["r_pname"],
                data["base"][0]["task_product"],
                data["base"][0]["r_position"],
                data["base"][0]["r_opportunity"],
                data["base"][0]["r_weldM"],
                data["base"][0]["r_type"],
                data["base"][0]["r_equ"],
                data["base"][0]["r_focus"],
                data["base"][0]["r_IQIV"],
                data["base"][0]["r_sensitizer"],
                data["base"][0]["r_filmBrand"],
                data["base"][0]["r_filmV"],
                data["base"][0]["r_devTime"],
                data["base"][0]["r_devCondition"],
                data["base"][0]["r_b"],
                data["base"][0]["r_standard"],
                data["base"][0]["r_qua"],
                data["base"][0]["r_heat"],
                data["base"][0]["r_texture"],
                data["base"][0]["r_equid"],
                data["base"][0]["r_develop"],
                data["base"][0]["r_devT"],
                data["base"][0]["r_tlevel"],
                data["base"][0]["r_check"],
                data["base"][0]["r_scale"],
                data["base"][0]["r_level"],
                data["base"][0]["r_remark"]
            ];
            if (postData["base"][0]["r_state"] == 1) {
                document.getElementById("verifyBox").setAttribute("style", "");
                document.getElementById("reason").innerHTML = "审核意见：" + postData["base"][0]["r_reason"];
            }
        }
    },
    error: function (data, status) {
        if (status == 'error') {
            alert("你的网络有问题！");
        }
    }
});
for (let i = 0; i < basicInfomationData[0].length; i++) {
    let tr = document.createElement("tr");
    if (i % 3 === 0) {
        basicInfomation.appendChild(tr);
    }
    let td = document.createElement("td");
    td.setAttribute("class", "td-shadows");
    td.setAttribute("align", "center");
    td.innerHTML = basicInfomationData[0][i];
    basicInfomation.children[basicInfomation.childElementCount - 1].appendChild(td);
    td = document.createElement("td");
    td.setAttribute("align", "center");
    td.innerHTML = basicInfomationData[1][i];
    basicInfomation.children[basicInfomation.childElementCount - 1].appendChild(td);
}

//显示工艺信息
var tecTable = document.getElementById("tecTable");
var tecTitle = ["工艺卡编号", "工艺规程/版本", "规格", "透照方式", "像质计灵敏度",
    "焦距", "一次透照长度", "分段数", "实拍数", "管电压", "管电流/源活", "曝光时间"];
var tecInfomation = [];
console.log(postData);
for (let i = 0; i < postData["tech"].length; i++) {
    tecInfomation.push([
        postData["tech"][i].tech_id,
        postData["tech"][i].tech_version,
        postData["tech"][i].w_spec,
        postData["tech"][i].tech_tranType,
        postData["tech"][i].tech_IQISen,
        postData["tech"][i].tech_focalDis,
        postData["tech"][i].tech_tranLen,
        postData["tech"][i].tech_seg,
        postData["tech"][i].sum,
        postData["tech"][i].w_V,
        postData["tech"][i].w_A,
        postData["tech"][i].tech_time,
    ]);
}
var tecInner = "";
for (let i = 0; i < tecTitle.length; i++) {
    tecInner += "<th>" + tecTitle[i] + "</th>";
}
tecTable.children[0].children[0].innerHTML = tecInner;
for (let i = 0; i < tecInfomation.length; i++) {
    let tr = document.createElement("tr");
    let inner = "";
    for (let j = 0; j < tecTitle.length; j++) {
        inner += "<td>" + tecInfomation[i][j] + "</td>";
    }
    tr.innerHTML = inner;
    tecTable.children[1].appendChild(tr);
}

//显示工件信息
var workpieceTable = document.getElementById("workpieceTable");
var workpieceTitle = ["部件编号", "焊缝编号", "片号","底片质量" ,"检测状态","缺陷位置", "缺陷尺寸", "缺陷性质", "评定结果", "备注"];
var workpieceInfomation = [];
var postData;
for (let i = 0; i < postData["photo"].length; i++) {
    workpieceInfomation.push([
        postData["photo"][i].w_pipeNum,
        postData["photo"][i].w_weldNum,
        postData["photo"][i].ph_phNum,
        postData["photo"][i].ph_quality,
        postData["photo"][i].ph_situation,
        postData["photo"][i].ph_pos,
        postData["photo"][i].ph_size,
        postData["photo"][i].ph_flaw,
        postData["photo"][i].ph_conclusion,
        postData["photo"][i].w_re == 0 ? "" : "第" + (postData["photo"][i].w_re + 1) + "次拍片"
    ]);
}

var workpieceInner = "";
for (let i = 0; i < workpieceTitle.length; i++) {
    workpieceInner += "<th>" + workpieceTitle[i] + "</th>";
    //let th = document.createAttribute("th");
    //th.innerHTML = workpieceTitle[i];
    //document.getElementById("workpieceTable").children[0].children[0].innerHTML(th);
}
workpieceTable.children[0].children[0].innerHTML = workpieceInner;
for (let i = 0; i < workpieceInfomation.length; i++) {
    let tr = document.createElement("tr");
    let inner = "";
    for (let j = 0; j < workpieceTitle.length; j++) {
        inner += "<td>" + workpieceInfomation[i][j] + "</td>";
    }
    tr.innerHTML = inner;
    workpieceTable.children[1].appendChild(tr);
}



//显示统计信息
var statTable = document.getElementById("statTable");
var statTitle = [" ", "Ⅰ级", "Ⅱ级", "Ⅲ级", "Ⅳ级"];
var statTitle_v = [];
var statInfomation = [];
var statData = [postData["photo"].length, postData["w_num"]];
document.getElementById("version2").innerHTML = "底片数量（总计）：" + statData[0];
document.getElementById("version3").innerHTML = "工件数量：" + statData[1];
var sum = [];
for (let i = 0; i < postData["photo"].length; i++) {
    if (sum[postData["photo"][i].w_re] == undefined) {
        sum[postData["photo"][i].w_re] = {};
        sum[postData["photo"][i].w_re][postData["photo"][i].ph_evalevel] = 1;
    }
    else sum[postData["photo"][i].w_re][postData["photo"][i].ph_evalevel] += 1
}
for (let i = 0; i < sum.length; i++) {
    statTitle_v[i] = "第" + (i + 1) + "次拍片";
    statInfomation[i] = sum[postData["photo"][i].w_re] == undefined ? [0, 0, 0, 0] : [
        sum[postData["photo"][i].w_re]["Ⅰ"] == undefined ? 0 : sum[postData["photo"][i].w_re]["Ⅰ"],
        sum[postData["photo"][i].w_re]["Ⅱ"] == undefined ? 0 : sum[postData["photo"][i].w_re]["Ⅱ"],
        sum[postData["photo"][i].w_re]["Ⅲ"] == undefined ? 0 : sum[postData["photo"][i].w_re]["Ⅲ"],
        sum[postData["photo"][i].w_re]["Ⅳ"] == undefined ? 0 : sum[postData["photo"][i].w_re]["Ⅳ"]
    ];
}
var statInner = "";
for (let i = 0; i < statTitle.length; i++) {
    statInner += "<th>" + statTitle[i] + "</th>";
}
statTable.children[0].children[0].innerHTML = statInner;
for (let i = 0; i < statInfomation.length; i++) {
    let tr = document.createElement("tr");
    let inner = "<td class=\"td-shadows\">" + statTitle_v[i] + "</td>";
    for (let j = 0; j < statTitle.length - 1; j++) {
        inner += "<td>" + statInfomation[i][j] + "</td>";
    }
    tr.innerHTML = inner;
    statTable.children[1].appendChild(tr);
}

//显示其他信息
var otherInfoID = ["m_name", "r_level", "r_date", "v_name", "r_verify_level", "r_verify_date", "a_name", "r_approve_level", "r_approve_date"];
var otherInfomationData = [[
    "编制人", "编制人级别", "编制日期",
    "审核人", "审核人级别", "审核日期",
    "批准人", "批准人级别", "批准日期"
], []];
for (let i = 0; i < otherInfoID.length; i++) {
    otherInfomationData[1][i] = postData["base"][0][otherInfoID[i]];
    if (i % 3 === 2) {
        var date
        postData["base"][0][otherInfoID[i]] == null ? date = new Date() : date = new Date(postData["base"][0][otherInfoID[i]])
        // var date = new Date(postData["base"][0][otherInfoID[i]]);
        console.log(postData["base"][0][otherInfoID[i]])
        postData["base"][0][otherInfoID[i]]=date.getFullYear() + "-" + (date.getMonth() + 1) + "-" + date.getDate();
        date.getDate();
        otherInfomationData[1][i] = postData["base"][0][otherInfoID[i]] == null ? "" : postData["base"][0][otherInfoID[i]];
    }
}
var otherInfomation = document.getElementById("otherInfomation").children[0];
for (let i = 0; i < otherInfomationData[0].length; i++) {
    let tr = document.createElement("tr");
    if (i % 3 === 0) {
        otherInfomation.appendChild(tr);
    }
    let td = document.createElement("td");
    td.setAttribute("class", "td-shadows");
    td.setAttribute("align", "center");
    td.innerHTML = otherInfomationData[0][i];
    otherInfomation.children[otherInfomation.childElementCount - 1].appendChild(td);
    td = document.createElement("td");
    td.setAttribute("align", "center");
    td.innerHTML = otherInfomationData[1][i];
    otherInfomation.children[otherInfomation.childElementCount - 1].appendChild(td);
}
//显示通过/不通过按钮
//审核人员审核报告,超级管理员审批报告

if (authority[18] == 1 && postData["base"][0]["r_state"] == 2){
    otherInfomation.children[2].children[1].innerHTML = user.user_name;
    otherInfomation.children[2].children[3].innerHTML = "<input id='r_approve_level'/>";
    document.getElementById("verify").setAttribute("style", "");
}else {
    if (authority[8] === 1 && postData["base"][0]["r_state"] == 0) {
        otherInfomation.children[1].children[1].innerHTML = user.user_name;
        otherInfomation.children[1].children[3].innerHTML = "<input id='r_verify_level'/>";
        document.getElementById("verify").setAttribute("style", "");
    }
}

// if (authority[6] === 1 && postData["base"][0]["r_state"] == 2) {//业务部门发布报告
//     otherInfomation.children[2].children[1].innerHTML = user.user_name;
//     otherInfomation.children[2].children[3].innerHTML = "<input id='r_verify_level'/>";
//     document.getElementById("verify").setAttribute("style", "");
// }

var sentMessage;
function OnCommit() {
    console.log('提交的数据为：' + JSON.stringify(sentMessage))
    if (postData["base"][0]["r_state"] == 0) {//审核状态为0表示未审核
        $.ajax({
            url: '/echo/report_check',
            type: 'post',
            data: sentMessage,
            async: false,
            success: function (data, status) {
                if (status == 'success') {
                    alert("提交成功");
                    history.back();
                }
            },
            error: function (data, status) {
                if (status == 'error') {
                    alert("你的网络有问题！");
                }
            }
        });
    }
    else if (postData["base"][0]["r_state"] == 2) { //审核通过，审批中。。
        $.ajax({
            url: '/echo/report_super_approve',
            type: 'post',
            data: sentMessage,
            async: false,
            success: function (data, status) {
                if (status == 'success') {
                    alert("提交成功");
                    history.back();
                }
            },
            error: function (data, status) {
                if (status == 'error') {
                    alert("你的网络有问题！");
                }
            }
        });
    }
}

function OnNoPass() {
    document.getElementById("noPass").setAttribute("onclick", "OnCommitNoPass();");
    document.getElementById("checkDescribe").setAttribute("class", "col-md-12");
    document.getElementById("noPass").innerHTML = "<i class=\"ace-icon fa fa-close bigger-120\">提交</i>";
}

function OnCommitNoPass() {
    var date = new Date();
    if (postData["base"][0]["r_state"] == 0) {//未审核
        sentMessage = {
            "r_id": r_id,
            "r_state": 1,//审核不通过
            "r_verify_person": user.user_id,
            "r_verify_level": document.getElementById("r_verify_level").value,
            "r_verify_date": date.getFullYear() + "-" + (date.getMonth() + 1) + "-" + date.getDate(),
            "r_reason": document.getElementById("reason").value,
            "requestType": "check"
        };
    }
    else {
        sentMessage = {
            "r_id": r_id,
            "r_state": 3,//审批不通过
            "r_approve_person": user.user_id,
            "r_approve_level": document.getElementById("r_approve_level").value,
            "r_approve_date": date.getFullYear() + "-" + (date.getMonth() + 1) + "-" + date.getDate(),
            "r_reason": document.getElementById("reason").value,
            "requestType": "check"
        };
    }
    OnCommit();
}

function OnPass() {
    var date = new Date();
    if (postData["base"][0]["r_state"] == 0) {
        sentMessage = {
            "r_id": r_id,
            "r_state": 2,//审核通过，批准中
            "r_verify_person": user.user_id,
            "r_verify_level": document.getElementById("r_verify_level").value,
            "r_verify_date": date.getFullYear() + "-" + (date.getMonth() + 1) + "-" + date.getDate(),
            "r_reason": "",
            "requestType": "check"
        };
    }
    else {
        sentMessage = {
            "r_id": r_id,
            "r_state": 4,//批准通过
            "r_approve_person": user.user_id,
            "r_approve_level": document.getElementById("r_approve_level").value,
            "r_approve_date": date.getFullYear() + "-" + (date.getMonth() + 1) + "-" + date.getDate(),
            "r_reason": "",
            "requestType": "check"
        };
    }
    OnCommit();
}